package edu.mtu.citizenscience.cs.sdk.uploader.db;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Interface for opening and closing the DB for the uploader
 */
public class UploaderDBInterface {
	// whether the DB is currently open or not
	private boolean open = false;
	// the open DB if any
	private SQLiteDatabase db;
	// the DB helper for opening the DB
	private UploadDBHelper dbHelper;
	// the context to interact with the DB with
	private Context context;
	
	/**
	 * Create the Uploader DB Interface
	 * 
	 * @param   context - Context - the context to open the DB with
	 * @param   dbName - dbName - the name of the DB to strip
	 * @param   version - int - the DB version
	 * @return	void
	 */
	public UploaderDBInterface(Context context, String dbName, int version)
	{
		this.context = context;
		this.dbHelper = new UploadDBHelper(this.context, dbName, version);
	}
	
	/**
	 * Open the DB
	 * 
	 * @param   void
	 * @return	void
	 * @throws SQLException
	 */
	public void openDB() throws SQLException
	{
		// check if the DB is not open
		if ( ! this.open)
		{
			// open the database as writable
			this.db = this.dbHelper.getWritableDatabase();
			// mark that the database is open
			this.open = true;
		}
	}
	
	/**
	 * Close the DB
	 * 
	 * @param   void
	 * @return	void
	 */
	public void closeDB()
	{
		// check if the DB is open
		if (this.open)
		{
			// close the database
			this.dbHelper.close();
			// mark that the database is not open
			this.open = false;
		}
	}
	
	/**
	 * Get the DB
	 * 
	 * @param   void
	 * @return	SQLiteDatabase - the DB
	 */
	public SQLiteDatabase getDB()
	{
		return this.db;
	}
	
}
